<?php
    set_include_path('../include');
    //error_reporting(E_ALL);
    require_once 'Zend/Loader/Autoloader.php';
    
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);
    
    // загрузка конфигурации приложения
    $config = new Zend_Config_Ini('../settings.ini', 'development');
    Zend_Registry::set('config', $config);
    
    // создание объекта для системного журнала
    $logger = new Zend_Log(new Zend_Log_Writer_Stream($config->logging->file));
    Zend_Registry::set('logger', $logger);
    
    // соединение с БД
    $params = array(
        'host'     => $config->database->hostname,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname'   => $config->database->database
    );
    
    $db = Zend_Db::factory($config->database->type, $params);
    Zend_Registry::set('db', $db);
    $db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
    
    // обработка запроса пользователя
    $controller = Zend_Controller_Front::getInstance();
    $controller->setControllerDirectory(
            $config->paths->base . '/include/Controllers');
    
    // настройки визуализатора
    $vr = new Zend_Controller_Action_Helper_ViewRenderer();
    $vr->setView(new Templater());
    
    $vr->setViewSuffix('html');
    
    Zend_Controller_Action_HelperBroker::addHelper($vr);
    
    // далее будем генерировать динамические роуты
    // это пипец а не процедура, я не знаю, как это переживет сервак...
    // но будем пробывать:)
    
    // получим список городов
    
    $router = $controller->getRouter();
    $route = new Router(
            '/*',
            array(
                'controller' => 'index',
                'action' => 'index',
            )
    );
    
    $route->addVariableMap(array('type' => getTypes(), 
                                 'city' => getCities(), 
                                 'operation' => array('arenda', 'prodazha'), 
                                 'object' => array('dom', 'kvartiry', 'zhilaja-nedvizhimost', 'kommercheskaya-nedvizhimost', 'ofis'), 
                                )
                          );
    
    $router->addRoute('index', $route);
    
    $router = $controller->getRouter();
    $route = new Router(
            '/search/*',
            array(
                'controller' => 'search',
                'action' => 'index',
            )
    );
    
    $router->addRoute('search', $route);
    
    $router = $controller->getRouter();
    $route = new Router(
            '/obj/*',
            array(
                'controller' => 'obj',
                'action' => 'index',
            )
    );
    
    $router->addRoute('obj', $route);
   /* $cities = getCities();
    $types = getTypes();
    foreach ($cities as $city)
    {
        // создадим роут для самого города
        $route = new Zend_Controller_Router_Route(
                $city . '/',
                array('controller' => 'index',
                    'action' => 'city')
        );

        $controller->getRouter()->addRoute($city, $route);
        
        // теперь для каждого района в городе        
        $regions = getRegions($city);
        
        foreach ($regions as $region)
        {
            $route = new Zend_Controller_Router_Route(
                    $city . '/' . $region,
                    array('controller' => 'index',
                        'action' => 'region')
            );

            $controller->getRouter()->addRoute($region, $route);
            
            // теперь для каждого операции с недвижимостью в районе
            foreach ($types as $type)
            {
                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda',
                        array('controller' => 'index',
                            'action' => 'regionarenda')
                );

                $controller->getRouter()->addRoute($city . $region .'arenda', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha',
                        array('controller' => 'index',
                            'action' => 'regionprodazha')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazha', $route);
                

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/kvartiri',
                        array('controller' => 'index',
                            'action' => 'regionarendakvartiri')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendakvartiri', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/doma',
                        array('controller' => 'index',
                            'action' => 'regionarendadoma')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendadoma', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/posutochno',
                        array('controller' => 'index',
                            'action' => 'regionarendaposutochno')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaposutochno', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/ofisi',
                        array('controller' => 'index',
                            'action' => 'regionarendaofisi')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaofisi', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/skladskie-pomeshcheniya',
                        array('controller' => 'index',
                            'action' => 'regionarendasklad')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendasklad', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/torgovie-pomeshcheniya',
                        array('controller' => 'index',
                            'action' => 'regionarendatorgovie')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendatorgovie', $route);


                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/garazhi',
                        array('controller' => 'index',
                            'action' => 'regionarendagarazhi')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendagarazhi', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/prochee',
                        array('controller' => 'index',
                            'action' => 'regionarendaprochee')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaprochee', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/snimu',
                        array('controller' => 'index',
                            'action' => 'regionarendasnimu')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendasnimu', $route);

                // роуты на продажу

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/kvartiri',
                        array('controller' => 'index',
                            'action' => 'regionprodazhakvartiry')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhakvartiri', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/doma',
                        array('controller' => 'index',
                            'action' => 'regionprodazhadoma')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhadoma', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/dachi',
                        array('controller' => 'index',
                            'action' => 'regionprodazhadachi')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhadachi', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/zemelnie-uchastki',
                        array('controller' => 'index',
                            'action' => 'regionprodazhazemelnieuchastki')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhazemelnieuchastki', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/ofisi',
                        array('controller' => 'index',
                            'action' => 'regionprodazhaofisi')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaofisi', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/torgovie-pomeshchenia',
                        array('controller' => 'index',
                            'action' => 'regionprodazhatorgoviepomeshchenia')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhatorgoviepomeshchenia', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/proizvodstva',
                        array('controller' => 'index',
                            'action' => 'regionprodazhaproizvodstva')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaproizvodstva', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/skladskie-pomeshcheniya',
                        array('controller' => 'index',
                            'action' => 'regionprodazhaskladskiepomeshchenia')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaskladskiepomeshchenia', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/obshchepit',
                        array('controller' => 'index',
                            'action' => 'regionprodazhaobshchepit')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaobshchepit', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/garazhi',
                        array('controller' => 'index',
                            'action' => 'regionprodazhagarazhi')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhagarazhi', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/prochee',
                        array('controller' => 'index',
                            'action' => 'regionprodazhaprochee')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaprochee', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/kuplyu',
                        array('controller' => 'index',
                            'action' => 'regionprodazhakuplyu')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhakuplyu', $route);
                
                //-----------------------------------------------------
                //---- далее будут перечислены роуты для объектов -----
                //-----------------------------------------------------               

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/kvartiri/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendakvartiriobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/doma/:object_id/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendadomaobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/posutochno/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaposutochnoobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/ofisi/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaofisiobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/arenda/skladskie-pomeshcheniya/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaskladobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/torgovie-pomeshcheniya/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendatorgovieobj', $route);


                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/garazhi/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendagarazhiobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/prochee/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendaprocheeobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/arenda/snimu/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'arendasnimuobj', $route);

                // роуты на продажу
                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/kvartiri/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhakvartiriobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/doma/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhadomaobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/dachi/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhadachiobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/zemelnie-uchastki/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhazemelnieuchastkiobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/ofisi/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaofisi', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/torgovie-pomeshchenia/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhatorgoviepomeshcheniaobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/proizvodstva/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaproizvodstvaobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city. '/' . $region . '/prodazha/skladskie-pomeshcheniya/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaskladskiepomeshcheniaobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/obshchepit/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaobshchepitobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/garazhi/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhagarazhiobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/prochee/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhaprocheeobj', $route);

                $route = new Zend_Controller_Router_Route(
                        $city . '/' . $region . '/prodazha/kuplyu/:objectId/*',
                        array('controller' => 'object',
                            'action' => 'index')
                );

                $controller->getRouter()->addRoute($city . $region . 'prodazhakuplyuobj', $route);
            }
        }
        
        //------------------------------------------------
        //------------------------------------------------
        //---------- роуты для аренды --------------------
        //------------------------------------------------
        //------------------------------------------------
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda',
                 array('controller' => 'index',
                       'action' => 'cityarenda')
        );

        $controller->getRouter()->addRoute($city . 'arenda', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/kvartiri',
                 array('controller' => 'index',
                       'action' => 'cityarendakvartiri')
        );

        $controller->getRouter()->addRoute($city . 'arendakvartiri', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/doma',
                 array('controller' => 'index',
                       'action' => 'cityarendadoma')
        );

        $controller->getRouter()->addRoute($city . 'arendadoma', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/posutochno',
                 array('controller' => 'index',
                       'action' => 'cityarendaposutochno')
        );

        $controller->getRouter()->addRoute($city . 'arendaposutochno', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/ofisi',
                 array('controller' => 'index',
                       'action' => 'cityarendaofisi')
        );

        $controller->getRouter()->addRoute($city . 'arendaofisi', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/skladskie-pomeshcheniya',
                 array('controller' => 'index',
                       'action' => 'cityarendasklad')
        );

        $controller->getRouter()->addRoute($city . 'arendasklad', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/torgovie-pomeshcheniya',
                 array('controller' => 'index',
                       'action' => 'cityarendatorgovie')
        );

        $controller->getRouter()->addRoute($city . 'arendatorgovie', $route);

        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/garazhi',
                 array('controller' => 'index',
                       'action' => 'cityarendagarazhi')
        );

        $controller->getRouter()->addRoute($city . 'arendagarazhi', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/prochee',
                 array('controller' => 'index',
                       'action' => 'cityarendaprochee')
        );

        $controller->getRouter()->addRoute($city . 'arendaprochee', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/arenda/snimu',
                 array('controller' => 'index',
                       'action' => 'cityarendasnimu')
        );

        $controller->getRouter()->addRoute($city . 'arendasnimu', $route);
        
        //-------------------------------------------
        //-------------------------------------------
        //----------- роуты на продажу --------------
        //-------------------------------------------
        //-------------------------------------------
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha',
                 array('controller' => 'index',
                       'action' => 'cityprodazha')
        );

        $controller->getRouter()->addRoute($city . 'prodazha', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/kvartiri',
                 array('controller' => 'index',
                       'action' => 'cityprodazhakvartiry')
        );

        $controller->getRouter()->addRoute($city . 'prodazhakvartiri', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/doma',
                 array('controller' => 'index',
                       'action' => 'cityprodazhadoma')
        );

        $controller->getRouter()->addRoute($city . 'prodazhadoma', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/dachi',
                 array('controller' => 'index',
                       'action' => 'cityprodazhadachi')
        );

        $controller->getRouter()->addRoute($city . 'prodazhadachi', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/zemelnie-uchastki',
                 array('controller' => 'index',
                       'action' => 'cityprodazhazemelnieuchastki')
        );

        $controller->getRouter()->addRoute($city . 'prodazhazemelnieuchastki', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/ofisi',
                 array('controller' => 'index',
                       'action' => 'cityprodazhaofisi')
        );

        $controller->getRouter()->addRoute($city . 'prodazhaofisi', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/torgovie-pomeshchenia',
                 array('controller' => 'index',
                       'action' => 'cityprodazhatorgoviepomeshchenia')
        );

        $controller->getRouter()->addRoute($city . 'prodazhatorgoviepomeshchenia', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/proizvodstva',
                 array('controller' => 'index',
                       'action' => 'cityprodazhaproizvodstva')
        );

        $controller->getRouter()->addRoute($city . 'prodazhaproizvodstva', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/skladskie-pomeshcheniya',
                 array('controller' => 'index',
                       'action' => 'cityprodazhaskladskiepomeshchenia')
        );

        $controller->getRouter()->addRoute($city . 'prodazhaskladskiepomeshchenia', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/obshchepit',
                 array('controller' => 'index',
                       'action' => 'cityprodazhaobshchepit')
        );

        $controller->getRouter()->addRoute($city . 'prodazhaobshchepit', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/garazhi',
                 array('controller' => 'index',
                       'action' => 'cityprodazhagarazhi')
        );

        $controller->getRouter()->addRoute($city . 'prodazhagarazhi', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/prochee',
                 array('controller' => 'index',
                       'action' => 'cityprodazhaprochee')
        );

        $controller->getRouter()->addRoute($city . 'prodazhaprochee', $route);
        
        $route = new Zend_Controller_Router_Route(
                 $city . '/prodazha/kuplyu',
                 array('controller' => 'index',
                       'action' => 'cityprodazhakuplyu')
        );

        $controller->getRouter()->addRoute($city . 'prodazhakuplyu', $route);
    }*/
    
    $controller->dispatch();

?>